<!--
 Copyright (c) 2010, Monash e-Research Centre
   (Monash University, Australia)
 Copyright (c) 2010, VeRSI Consortium
   (Victorian eResearch Strategic Initiative, Australia)
 All rights reserved.
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
    *  Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    *  Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    *  Neither the name of the VeRSI, the VeRSI Consortium members, nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
{% extends "portal_template.html" %} 
{% load basiccomparisonfilters %}
{% load parametertypecheck %}
		
		{% block script %}
		
		<script language="JavaScript">

		function disableEnterKey(e)
		{
		     var key;     
		     if(window.event)
		          key = window.event.keyCode; //IE
		     else
		          key = e.which; //firefox     

		     return (key != 13);
		}

		</script>		
		
		<script type="text/javascript">
			$(function() {
				$("#tabs").tabs();
			});
		</script>

		<script type="text/javascript">
			$(document).ready(function() {	
				
				$('.filename_search').live('keyup', function(e) {
					e.preventDefault();
					if(e.keyCode == 13) {
						dataset_id = $(this).siblings('.dataset_id').val();

						$(this).parents('.datafile_list').load("/ajax/datafile_list/" + dataset_id + "/?filename=" + $(this).val(), function(){
								$(document).ready(function(){
										// datafile metadata
										$(".datafile_info_toggle").unbind('click');
										$(document).find('.datafile_info_toggle').text("[Show Metadata]");
										$(document).find('.datafile_info').hide();
										 $(document).find('.datafile_info_toggle').toggle(function(){
							 				$(this).text("[Hide Metadata]");
											var href = $(this).attr("href");
											$(this).next().show();

											//if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
											//{
												$(this).next().html(loadingHTML);									
											//}
											$(this).next().load(href);

										 },function(){
											$(this).text("[Show Metadata]");
											$(this).next().hide();
										 });
										
										$(".dataset_selector_all").unbind('click');
										$(".dataset_selector_none").unbind('click');
										// file selectors
										$(document).find('.dataset_selector_all').click(function() {	
											$(this).parent().find('.datafile_checkbox').attr("checked", "checked");

									   });

										$(document).find('.dataset_selector_none').click(function() {	
											$(this).parent().find('.datafile_checkbox').removeAttr("checked");

									   });											
									});
						
								});
						}

					});
						
				
				// show hide various metadata divs
				
			   $('#datasets').find('.dataset_info').end().find('h2').click(function() {
			     $(this).next().toggle();
			   });

			   $('.dataset_info').find('.datafile_list').hide().end().find('.datafile_list_toggle').click(function() {
			     $(this).next().toggle();
			   });	


				$('.pagelink').live('click', function( event ){
							var href = $(this).attr("href");
							html = $(this).html();
							$(this).html(html + " " + loadingHTML);
							$(this).parents('.datafile_list').load(href, function(){
									$(document).ready(function(){
											// datafile metadata
											$(".datafile_info_toggle").unbind('click');
											$(document).find('.datafile_info_toggle').text("[Show Metadata]");
											$(document).find('.datafile_info').hide();
											 $(document).find('.datafile_info_toggle').toggle(function(){
								 				$(this).text("[Hide Metadata]");
												var href = $(this).attr("href");
												$(this).next().show();

												//if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
												//{
													$(this).next().html(loadingHTML);									
												//}
												$(this).next().load(href);

											 },function(){
												$(this).text("[Show Metadata]");
												$(this).next().hide();
											 });
											
											$(".dataset_selector_all").unbind('click');
											$(".dataset_selector_none").unbind('click');
											// file selectors
											$(document).find('.dataset_selector_all').click(function() {	
												$(this).parent().find('.datafile_checkbox').attr("checked", "checked");

										   });

											$(document).find('.dataset_selector_none').click(function() {	
												$(this).parent().find('.datafile_checkbox').removeAttr("checked");

										   });											
										});
														
							//$(this).html(html);
							
						})
						return false;
					});
			
			// default text set
			
			$(".dataset_metadata_toggle").text("[Show]");	
			$(".dataset_metadata_toggle").next().hide();
			$(".datafile_list_toggle").text("[Show]");		
			
			var loadingHTML = "<img src='/site_media/images/ajax-loader.gif'/>";

			// metadata text / ajax toggle

			// dataset md
			 $(".dataset_metadata_toggle").toggle(function(){
			   $(this).text("[Hide]");
			    var href= $(this).attr("href");
				$(href).show();				
			
			 },function(){
			   $(this).text("[Show]");
			    var href= $(this).attr("href");
				$(href).hide();
			 });
			
			// datafile list
			 $(".datafile_list_toggle").toggle(function(){
			   $(this).text("[Hide]");
				var href = $(this).attr("href");

				$(this).next().html(loadingHTML + "</br>");									
				
				var datafile_info_container = $(this).next();
				
				// add initialisation for new datafile handlers as callback once ajax has loaded
				// damn I love jQuery
				$(this).next().load(href,function(){
						$(document).ready(function(){						
							
							var datafile_info_selectors = 	
								datafile_info_container.find('.datafile_info').hide().end().find('.datafile_info_toggle');
							
							datafile_info_selectors.text("[Show Metadata]");

							// datafile metadata
							 datafile_info_selectors.toggle(function(){
				 				$(this).text("[Hide Metadata]");
								var href = $(this).attr("href");

								//if($(this).next().text().replace(/^\s+|\s+$/g,'') == "")
								//{
									$(this).next().html(loadingHTML);									
								//}
								$(this).next().load(href);
															
							 },function(){
								$(this).text("[Show Metadata]");
							 });	
							
							   datafile_info_selectors.click(function() {
								$(this).next().toggle();				
							   });		
							
								// file selectors
								datafile_info_container.find('.dataset_selector_all').click(function() {	
									$(this).parent().find('.datafile_checkbox').attr("checked", "checked");

							   });
							
								datafile_info_container.find('.dataset_selector_none').click(function() {	
									$(this).parent().find('.datafile_checkbox').removeAttr("checked");

							   });	
							
						});
					 });			
			 },function(){
			   $(this).text("[Show]");
			 });

			});
		</script>
		
		{% endblock %}
		
		{% block fullpage %}

		<div id="fullpage">
			<div class="post">
				<h1 class="title">
					{{ experiment.title }} 
							{% if experiment.pdbid_set.count %}
								<em>({% for pdbid in experiment.pdbid_set.all %}{{ pdbid.pdbid }}{% if not forloop.last %}, {% endif %}{% endfor %})</em>							
							{% endif %}
					
				</h1>
				{% if experiment.mytardis_id %}
				<p>
					This experiment is a MyTARDIS entry from {{experiment.url}}
				</p>
				{% endif %}

					<div class="demo">

					<div id="tabs">
						<ul>
							<li><a href="#tabs-1">Description</a></li>
							<li><a href="#tabs-2">Datasets ({{ experiment.dataset_set.count }})</a></li>						
							{% if experiment.ftp_location %}<li><a href="/ajax/ftp/{{experiment.id}}">FTP Download</a></li>{% endif %}
						</ul>
						<div id="tabs-1">							
							
                            <p>
                            <strong>Authors: </strong><br/>
                            {% for author in authors %}
                                    {{ author.author.name }}{% if not forloop.last %},{% endif %}
                            {% endfor %}
                            </p>

							<p>
							<strong>Abstract: </strong>
							<div class="abstract">
							{{ experiment.description|safe }}					
							</div>
							</p>
							<p>
							{% if experiment.pdbid_set.count %}
							<strong>PDB id(s):</strong>
							{% for pdbid in experiment.pdbid_set.all %}
							{{ pdbid.pdbid }}{% if not forloop.last %},{% endif %}
							{% endfor %}
							{% endif %}
							</p>
							<p>
							{% if experiment.citation_set.count %}
							<strong>Citations:</strong><br/>
							{% for citation in experiment.citation_set.all %}
							<a href="{{ citation.url }}">{{ citation.url }}</a><br/>
							{% endfor %}
							{% endif %}
							</p>
							
							{% if experiment.handle %}
							<p>
							<strong>Persistent Handle:</strong>
							<a href="{{ handle }}/{{ experiment.handle }}">{{ experiment.handle }}</a><br/>							
							</p>							
							{% endif %}
							
							<p>
							<strong>Institution:</strong>
							{{experiment.institution_name}}<br/>							
							</p>
							
							<p>
								<strong>Dataset Information:</strong>
								<div>
									<!-- <strong>Dataset Metadata: </strong>	 -->
									<div class="dataset_information">
										<ul>
												<li><strong>Datasets:</strong> {{experiment.dataset_set.all.count}}</li>
												<li><strong>Files:</strong> {{datafiles.count}}</li>
										</ul>										
									</div>								
								</div>
							</p>							

							<p>
							<strong>Experiment Last Updated:</strong>
							{{ experiment.update_time|date:"jS F Y H:i" }}<br/>							
							</p>																												
							
						</div>
						<div id="tabs-2">
							
							{% if experiment.mytardis_id %}
							<p>
							<strong><a href="{{experiment.url}}experiment/view/{{experiment.mytardis_id}}/downloadExperiment/">Download Entire Experiment</a></strong><br/>
							</p>
							<form method="POST" action="{{experiment.url}}downloadTar/" target="_blank">
							<input type="hidden" name="expid" value="{{experiment.mytardis_id}}">
							{% endif %}		
																	
							<div id="datasets">
							{% for dataset in experiment.dataset_set.all %}
								
								<div class="dataset_title">
									<strong><span style="color: #5e5e5e">DATASET {{ forloop.counter }}</span></strong>
								</div>
								 
								<div class="dataset_info">
									
									<div class="dataset_description">
									<strong>Dataset Description</strong>: {{ dataset.description|safe }}	
									</div>
									
									<div class="dataset_extrainfo">
										{% if dataset.datasetparameter_set.count %}
										<strong>Dataset Metadata: </strong><a class="dataset_metadata_toggle" href="#dataset_metadata_{{forloop.counter}}">[Hide]</a>	
										<div class="dataset_metadata" id="dataset_metadata_{{forloop.counter}}">
										<ul>
												{% for parameter in dataset.datasetparameter_set.all %}
													<li><strong>{{ parameter.name.full_name }}: </strong>
														{% if parameter.name.is_numeric %}
															{{ parameter.numerical_value }} {{ parameter.name.units }}
														{% else %}
															{{ parameter.string_value|dsparametertypecheck:parameter.id|safe }}
														{% endif %}
													</li>
												{% endfor %}
										</ul>	
										</div>	
										{% endif %}					
								
										<div>	
											<strong>Data Files ({{ dataset.dataset_file_set.count }}): </strong>
											<a {% if dataset.dataset_file_set.count|lt:"1000" %}class="datafile_list_toggle"{% else %}target="_blank"{% endif %} href="/ajax/datafile_list/{{ dataset.id }}">[Show]</a>					
											{% if dataset.dataset_file_set.count|gt:"1000" %}
											<br/>
											<p><em><strong>Note:</strong> Due to large dataset, files will load in a new window</em></p>
											{% endif %}	
											
											<ul class="datafile_list">
									
											</ul>
											
										</div>
									</div>
								</div>
							{% endfor %}
							{% if experiment.mytardis_id %}
							</div>		
							<input type="submit" value="Download Selected Files" />
							</form>		
							{% endif %}					
							</div>												
						</div>		
																		
					</div>

					</div><!-- End demo -->		

			</div>
			</div>

			{% endblock %}